package com.base.common.aop.configure;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.service.base.system.service.ConfigureService;
import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.converter.AbstractConverterEditor;

/**
 * 配置AOP
 * @author mac
 *
 */
@Aspect
@Component
public class ConfigureBeanAspect {

	@Autowired
	private ConfigureService configureService;
	
	@Pointcut("within(@com.base.core.head.annotations.AutoConfigure *)")
    private void autoConfigure() {
    }

    @Around("autoConfigure()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
		String methodName=method.getName();
		if(method.getReturnType().equals(Void.class)||
				!methodName.startsWith("get")||
				joinPoint.getArgs().length>0) {
			return joinPoint.proceed();
		}
		String fieldName=methodName.substring(3,4).toLowerCase()+methodName.substring(4);
		Field field=joinPoint.getTarget().getClass().getDeclaredField(fieldName);
		AbstractConverterEditor<?> convertObj=ConverterEditorUtils.getFieldConverterEditor(field, null, field.getType());
		convertObj.restore(configureService.getString(fieldName));
    	return convertObj.getValue();
    }
	
}
